package jp.bgbt.fles.app.bluetooth;

import org.greenrobot.eventbus.EventBus;

import jp.bgbt.fles.app.bean.EventMsg;


public class BleDeviceInfo {

    private static BleDeviceInfo instance = new BleDeviceInfo();
    private String deviceMac;
    private String deviceRandom;
    private String wifiAddr;
    private String deviceVersion;
    private boolean deviceConnected;
    private boolean canSendNextCommand;
    private boolean deviceWifiConnected;
    private boolean connectingWifi;
    private boolean inAnalysisResult;
    private boolean isShowingResult;
    private int remainEle;


    private int nurseType;
    private int nurseState;//护理状态 (0未开始护理,1暂停中,2护理中)
    private int recordId;
    private int usedTime; //设备已使用总时长

    public static BleDeviceInfo getInstance() {
        return instance;
    }

    public void init(String deviceMac){
        setDeviceConnected(true);
        setDeviceMac(deviceMac);
        setWifiAddr("");
        setDeviceRandom("");
        setRecordId(-1);
        setRemainEle(-1);
    }

    public boolean isCanSendNextCommand() {
        return canSendNextCommand;
    }

    public void setCanSendNextCommand(boolean canSendNextCommand) {
        this.canSendNextCommand = canSendNextCommand;
    }

    public boolean isDeviceConnected() {
        return deviceConnected;
    }

    public void setDeviceConnected(boolean deviceConnected) {
        this.deviceConnected = deviceConnected;
    }

    public String getDeviceMac() {
        return deviceMac;
    }

    public void setDeviceMac(String deviceMac) {
        this.deviceMac = deviceMac;
    }

    public String getDeviceRandom() {
        return deviceRandom;
    }

    public void setDeviceRandom(String deviceRandom) {
        this.deviceRandom = deviceRandom;
    }

    public int getNurseState() {
        return nurseState;
    }

    public void setNurseState(int nurseState) {
        //初始状态，读当前电压
        if(this.nurseState == BleConstant.NURSE_INIT
                && BleDeviceInfo.getInstance().isDeviceConnected()) {
            BleCommandUtil.getInstance().readEle();
        } else if(this.nurseState == BleConstant.NURSE_RUNNING
                && BleDeviceInfo.getInstance().isDeviceConnected()) {
            BleCommandUtil.getInstance().readEleDelay();
        }
        if(this.nurseState == BleConstant.NURSE_RUNNING && nurseState == BleConstant.NURSE_INIT) {
            EventBus.getDefault().post(new EventMsg(EventMsg.MSG_REFRESH_USER_INFO));
        }
        this.nurseState = nurseState;
    }

    public int getNurseType() {
        return nurseType;
    }

    public void setNurseType(int nurseType) {
        this.nurseType = nurseType;
    }

    public String getWifiAddr() {
        return wifiAddr;
    }

    public void setWifiAddr(String wifiAddr) {
        if(wifiAddr.equals(this.wifiAddr))
            return;
        this.wifiAddr = wifiAddr;
        setDeviceWifiConnected(false);
    }

    public boolean isDeviceWifiConnected() {
        return deviceWifiConnected;
    }

    public void setDeviceWifiConnected(boolean deviceWifiConnected) {
//        LogUtil.e("设置连接设备wifi状态 " + deviceWifiConnected);
        this.deviceWifiConnected = deviceWifiConnected;
    }

    public boolean isInAnalysisResult() {
        return inAnalysisResult;
    }

    public void setInAnalysisResult(boolean inAnalysisResult) {
        this.inAnalysisResult = inAnalysisResult;
    }

    public boolean isShowingResult() {
        return isShowingResult;
    }

    public void setShowingResult(boolean showingResult) {
        isShowingResult = showingResult;
    }

    public int getRecordId() {
        return recordId;
    }

    public void setRecordId(int recordId) {
        this.recordId = recordId;
    }

    public String getDeviceVersion() {
        return deviceVersion;
    }

    public void setDeviceVersion(String deviceVersion) {
        this.deviceVersion = deviceVersion;
    }

    public int getRemainEle() {
        return remainEle;
    }

    public void setRemainEle(int remainEle) {
        this.remainEle = remainEle;
    }

    public int getUsedTime() {
        return usedTime;
    }

    public boolean isConnectingWifi() {
        return connectingWifi;
    }

    public void setConnectingWifi(boolean connectingWifi) {
        this.connectingWifi = connectingWifi;
    }

/*
    public void setUsedTime(int usedTime) {
        if(usedTime == this.usedTime)
            return;
        this.usedTime = usedTime;
        CareTimeBean bean = new CareTimeBean();
        bean.setInsertTime(System.currentTimeMillis());
        bean.setCurrentUsedTime(usedTime);
        bean.setDeviceMac(deviceMac);
        try {
            DBManager.getInstance().insertCareTimeBean(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
*/

    public String getRealWifiSSID() {
        boolean startWithFLES = wifiAddr.startsWith("F0");
        String tempSSID = wifiAddr.substring(2);
        String ssid = startWithFLES ? "FLES-SELF_" + tempSSID : "MLC_" + wifiAddr;
        return ssid;
    }
}
